<%@ page import="com.actimind.actitime.beans.UserBean,
                 com.actimind.actitime.Keys,
                 com.actimind.actitime.AccessRights,
                 java.sql.Connection,
                 java.util.*,
                 com.actimind.actitime.resources.UserResource,
                 com.actimind.actitime.beans.AccessRightBean,
                 com.actimind.actitime.util.CapsuleFactory,
                 com.actimind.actitime.util.Capsule,
                 java.sql.SQLException,
                 org.apache.struts.util.ResponseUtils"%><%@ page errorPage="/error.jsp" %><%

UserBean loggedInUser = (UserBean)session.getAttribute(Keys.LOGGEDIN_USER_KEY);
if (loggedInUser != null)
{
    Capsule c = new Capsule(null);

    try
    {
        UserResource ur = new UserResource();

        Connection con = c.getConnection();

        if (loggedInUser.isRoot() || ur.hasUserRight(con, loggedInUser.getId(), "system.manage_users"))
        {
            Map rights = ur.getAllRights(con);
            List usersList  = ur.getAllUsers(con);

%>
<%@ taglib uri="/WEB-INF/at.tld" prefix="at" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<html>
<head>
    <title><bean:write name="title-prefix" scope="application"/> User List</title>

    <link rel="stylesheet" href="../css/at.css" type="text/css">
</head>

<body bgcolor="#ffffff" topmargin="0" leftmargin="0" marginheight="0" marginwidth="0">

<jsp:include page="../top.jsp" flush="false">
    <jsp:param name="pageSection" value="administration" />
    <jsp:param name="pageAction" value="userlist" />
</jsp:include>

<form>
<table cellspacing="0" cellpadding="5" align="center" valign="top" border="0" bgcolor="#ffffff">
<tr>
    <td class="pagetitle">
        User List
    </td>
    <td align="right">
            <input type="button" value="Add New User" onclick="javascript:document.location.href='<html:rewrite page="/administration/useradd.do"/>'" />
    </td>
</tr>
<tr>
    <td colspan="2" class="pagetitle">
        <at:errors />
        <at:messages id="message" message="true">
            <span class="successmsg"><bean:write name="message" /></span>
        </at:messages>
    </td>
</tr>
<tr>
    <td colspan="2">
        <table border="0" cellspacing="1" cellpadding="5"  align="center" width="770">
        <tr>
            <th class="listtblcolheader" rowspan="2" align="left">User</th>
            <th class="listtblcolheadersmall" colspan="<%=rights.size()%>">Access Rights</th>
            <th class="listtblcolheader" rowspan="2">Workday Duration</th>
            <th class="listtblcolheader" rowspan="2">Access</th>
        </tr>
        <tr>
<%
            for (int i = 0; i < AccessRights.ORDERED_RIGHTS.length; i++)
            {
%>            <th class="listtblcolheadersmall"><%=ResponseUtils.filter(((AccessRightBean)rights.get(AccessRights.ORDERED_RIGHTS[i])).getDescription())%></th>
<%
            }

            Iterator it = usersList.iterator();

            UserBean user = null;

            Set userRightsSet = null;

            while (it.hasNext())
            {
                user = (UserBean)it.next();

                if (user.isRoot())
                    continue;

                userRightsSet = ur.getUserRights(con, user.getId());

%>
        </tr>
        <tr>
<%
                if (loggedInUser.getId() != user.getId())
                {
%>            <td class="listtblcell" nowrap="1"><html:link href='<%="useredit.do?userId=" + user.getId()%>'><%=ResponseUtils.filter(user.getFormattedName())%></html:link></td>
<%
                }
                else
                {
%>            <td class="listtblcell" nowrap="1"><%=ResponseUtils.filter(user.getFormattedName())%></td>
<%
                }

                for (int i = 0; i < AccessRights.ORDERED_RIGHTS.length; i++)
                {
                    if (userRightsSet.contains(rights.get(AccessRights.ORDERED_RIGHTS[i])))
                    {
%>
            <td class="listtblcell" align="center"><img src="../img/check.gif" width="15" height="15" /></td>
<%
                    }
                    else
                    {
%>
            <td class="listtblcell" align="center">&nbsp;</td>
<%
                    }
                }

                if(user.isOvertimeTrackingEnabled())
                {
                    String time;
                    if(user.getWorkdayDuration() > 0) {
                        String hour = new Integer(new Double(Math.floor(user.getWorkdayDuration() / 60)).intValue()).toString();
                        String minute = (user.getWorkdayDuration() % 60 == 0) ? "00" : "30";
                        time = hour + ":" + minute;
                    } else
                    {
                        time = "(default)";
                    }
%>
            <td class="listtblcell" align="center"><%=time%></td>
<%

                } else {
%>
            <td class="listtblcell" align="center">--</td>
<%                }

                if (user.isActive())
                {
%>            <td class="listtblcellenabled">enabled</td>
<%
                }
                else
                {
%>            <td class="listtblcelldisabled">disabled</td>
<%
                }
%>
        </tr>
<%
            }
%>
        </table>
    </td>
</tr>
</table>
</form>

</body>
</html>
<%
        }
        else
        {
            response.sendRedirect("../login.do");
        }
    }
    finally
    {
        if (c != null)
            c.cleanup();
    }
}
else
{
    response.sendRedirect("../login.do");
}
%>